<template>
  <div class="PhotoRecordDialog h100" v-loading="loading">
  <div class="PageContent">
    <el-dialog title="人脸识别" :visible.sync="$parent.DialogPhotoRecord" width="80%" v-dialogDrag border @close="closeDialog()">
    <el-button type="success" icon="el-icon-plus" @click="readCard()">读身份证 </el-button>
    <el-button type="success" icon="el-icon-plus" @click="cam_switch_dialog(2)">打开摄像头 </el-button>
    <el-button type="success" icon="el-icon-plus" @click="initFI_dialog()">人脸比对初始化 </el-button>
    <el-button type="success" icon="el-icon-plus" @click="FaceDete_dialog()">人脸检测 </el-button>
      <el-button type="success" icon="el-icon-plus" @click="bbase64_dialog()">摄像头拍照 </el-button>
    <el-button type="success" icon="el-icon-plus" @click="Base64Compare_Curr()">人脸比对 </el-button>
    <el-button type="success" icon="el-icon-plus" @click="PersonCompare()">人工比对 </el-button>
    <table align="center" border="1" cellpadding="1" cellspacing="1">
      <tr>
        <td height="80">
          <div id="idTips" align="center"></div>
        </td>
      </tr>
      <tr>
        <td>
          操作提示:<textarea id="Console_dialog" style="height:100px;width:400px;resize:none" disabled="disabled"></textarea>
        </td>
      </tr>
      <tr>
        <td>
          <div align="center">基本操作</div>
        </td>
        <td align="center" height = "70">

        </td>
      </tr>
      <tr>
        <td align="left">
          <canvas id="cam" width="640" height="480" value="123" style="border:1px solid #000000;">Your browser does not support the canvas element.</canvas>

        </td>
         <td align="center" width="100%" height="100%">
             <IMG ID="Base64IMG" height="180" width="220" src=""/>
             <IMG ID="Base64IMG_Person" height="320" width="420" src=""/>
          </td>
      </tr>
    </table>

    <div class="PageItem" >





  </div>
    </el-dialog>
  </div>
  </div>
</template>


<script>
   import fsIdCard from '@/api/FS1000A3TDIdCard' //慧影多浏览器引入脚本
   import fs1000a3tdV38   from '@/api/FS1000A3TD_Contrast'//多浏览器引入新脚本
   import axios from "axios"
   import store from '@/store'
   import { getToken } from '@/utils/auth'
   // import  fs1000a3td   from '@/api/FS1000A3TD'//多浏览器引入脚本

    export default {
        name: "PhotoRecordDialog",
        props: {
          DialogPhotoRecord: {
          type: Boolean
        },businessCode:""
       },
      data() {return {
        loading:false,//加载进度
        bFIInit:false,
        fThreshold:0.7,//验证门限
        strTipsType:"StartReadCard",
        timerFlag1:false,//提示牌定时器标志
        timerFlag2:false,//身份证阅读定时器标志
        bFIInit:false,//人脸模块初始化标志
        bRCInit:false,//身份证模块初始化标志
        bIsGrayVideoInit:false,//控制活体检测模式
        bTipFlash:true,//控制提示是否闪烁
        FLFICtrl:{},//active对象
        base64_1:"",//身份证照片
        base64_2:"",//承办人头像
        contrast:0//0未完成拍照1：完成身份证拍照2：完成承办人拍照，3比对成功

       }
      },
      methods: {
        // 单元格的 style 的回调方法
        FIStartDetectFace_Curr() {
          // fsPerson.FIStartDetectFace();
         },//读身份证
          FIReadCard_Curr(){
            fs1000a3tdV38.FIReadCard_C1();
          }
        ,//base64拍照
        BB64(){
          //base64截图
          // fs1000a3td.camera.bbase64();
        },//新增script脚本
        appScript:function () {
          var ring = document.createElement("script");
          ring.setAttribute("for","FLFICtrl");
          ring.event="OnIdentifyFace()";
          ring.appendChild(document.createTextNode("phoneListener.ringUp()"))
          document.body.appendChild(ring)
        },
        check(){
          var fFaceScore = FLFICtrl.FaceScore.toFixed(6);
          var fColorLiveScore = FLFICtrl.ColorLiveScore.toFixed(6);
          var fGrayLiveScore = FLFICtrl.GrayLiveScore.toFixed(6);
          var nVerifyStatus = FLFICtrl.VerifyStatus;
        },
        addEvent(){
          $('body').bind('OnIdentifyFace', function (event) {
            var fFaceScore = FLFICtrl.FaceScore.toFixed(6);
            var fColorLiveScore = FLFICtrl.ColorLiveScore.toFixed(6);
            var fGrayLiveScore = FLFICtrl.GrayLiveScore.toFixed(6);
            var nVerifyStatus = FLFICtrl.VerifyStatus;

            var FaceScoreValue;
            if(fFaceScore == -10001)
            {
              FaceScoreValue = "未加载到照片";
            }else if(fFaceScore == -10002)
            {
              FaceScoreValue = "未检测到照片人脸";
            }else if(fFaceScore == -10003)
            {
              FaceScoreValue = "未提取到照片人脸特征";
            }else if(fFaceScore == -20001)
            {
              FaceScoreValue = "未检测到视频人脸";
            }else if(fFaceScore == -20002)
            {
              FaceScoreValue = "未提取到视频人脸特征";
            }else{
              FaceScoreValue = fFaceScore;
            }

            var ColorLiveScoreValue;
            if(fColorLiveScore == -10001)
            {
              ColorLiveScoreValue = "未加载到照片";
            }else if(fColorLiveScore == -10002)
            {
              ColorLiveScoreValue = "未检测到照片人脸";
            }else if(fColorLiveScore == -10003)
            {
              ColorLiveScoreValue = "未提取到照片人脸特征";
            }else if(fColorLiveScore == -20001)
            {
              ColorLiveScoreValue = "未检测到视频人脸";
            }else if(fColorLiveScore == -20002)
            {
              ColorLiveScoreValue = "未提取到视频人脸特征";
            }else if(fColorLiveScore == -1)
            {
              ColorLiveScoreValue = "未开启";
            }else{
              ColorLiveScoreValue = fColorLiveScore;
            }

            var GrayLiveScoreValue;
            if(fGrayLiveScore == -10001)
            {
              GrayLiveScoreValue = "未加载到照片";
            }else if(fGrayLiveScore == -10002)
            {
              GrayLiveScoreValue = "未检测到照片人脸";
            }else if(fGrayLiveScore == -10003)
            {
              GrayLiveScoreValue = "未提取到照片人脸特征";
            }else if(fGrayLiveScore == -20001)
            {
              GrayLiveScoreValue = "未检测到视频人脸";
            }else if(fGrayLiveScore == -20002)
            {
              GrayLiveScoreValue = "未提取到视频人脸特征";
            }else if(fGrayLiveScore == -1)
            {
              GrayLiveScoreValue = "未开启";
            }else{
              GrayLiveScoreValue = fGrayLiveScore;
            }

            document.getElementById("idColorIdfyScore").value = FaceScoreValue;
            document.getElementById("idColorLiveScore").value = ColorLiveScoreValue;
            document.getElementById("idGrayLiveScore").value = GrayLiveScoreValue;

            if(nVerifyStatus == 0)//在验证中
            {
              if(fFaceScore == -20001)
              {
                strTipsType = "DetectingVideoFace";//在验证中视频未发现人脸
              }else if(fFaceScore == -20002)
              {
                strTipsType = "GettingVideoFaceFeature";//在验证中视频提取人脸特征失败
              }else{
                strTipsType = "IndentifyingFail";//在验证中分数不够或者活体未通过
              }
            }else if(nVerifyStatus == 1)//验证通过
            {
              strTipsType = "FaceIdentifySuccess";
              setTimeout(function(){
                strTipsType = "StartReadCard";
                ClearInfo();
                clearInterval(timerFlag2);
                timerFlag2 = setInterval("AutoReadCard()",500);
              },5000);
            }else if(nVerifyStatus == -1)//验证失败
            {
              if(fFaceScore == -10001)
              {
                strTipsType = "LoadPhotoFail";//加载证件照片失败验证失败
              }else if(fFaceScore == -10002)
              {
                strTipsType = "GetPhotoFaceFail";//验证失败照片没找到人脸
              }else if(fFaceScore == -10003)
              {
                strTipsType = "GetPhotoFaceFeatureFail";//验证失败照片人脸特征提取失败
              }else if(fFaceScore == -20001)
              {
                strTipsType = "GetVideoFaceFail";//验证失败视频中没找到人脸
              }else if(fFaceScore == -20002)
              {
                strTipsType = "GettVideoFaceFeatureFail";//验证失败视频提取人脸特征失败
              }else{
                strTipsType = "IndentifyFail";//验证失败分数不够或者活体未通过
              }
              //复位
              setTimeout(function(){
                strTipsType = "StartReadCard";
                ClearInfo();
                clearInterval(timerFlag2);
                timerFlag2 = setInterval("AutoReadCard()",500);
              },5000);
            }
            console.log(typeof event);        // object
          });

        }, closeDialog() {
          this.$parent.PhotoRecordFlag = false
        },//读取身份证图像
        readCard(){
          let this_=this;
          fsIdCard.FsIdCard.ReadIDCard();
          setTimeout(function() {
            //获得读取的数据，主要取图像
            let msg = fsIdCard.FsIdCard.getMsg();
            document.getElementById("Base64IMG").src = "data:image/jpg;base64," + msg.photoDisplay;
            //完成拍照1
            this_.base64_1=msg.photoDisplay;
            this_.contrast=1;
            //生成文件
            this_.base64tofile();

          },1000);
        } ,//打开摄像头1，打开身份读卡器，2：打开摄像头
        cam_switch_dialog(v){
          fs1000a3tdV38.camera.cam_switch(v);
        },//初始化人脸比对
        initFI_dialog(){
          fs1000a3tdV38.camera.InitFI();
        },bb_dialog(){
          fs1000a3tdV38.camera.bb()
        },base64tofile(){
          try{
            let dataurl="data:image/jpg;base64,"+this.base64_1;
            let filename="D:\\001.jpg";
            var arr = dataurl.split(','),
              mime = arr[0].match(/:(.*?);/)[1],
              bstr = atob(arr[1]),
              n = bstr.length,
              u8arr = new Uint8Array(n);
            while (n--) {
              u8arr[n] = bstr.charCodeAt(n);
            }
            let file= new File([u8arr], filename, { type: mime });
            console.log(file);
          }catch(e){
            console.debug(e);
          }

        },
        //base4拍照
        bbase64_dialog(){
          let this_dialog=this;
          fs1000a3tdV38.camera.bbase64Camare();
          setTimeout(function() {
            //完成拍照2
            let msg= fs1000a3tdV38.camera.getBase64String();
            this_dialog.base64_2=msg;
            this_dialog.contrast=2;
          },1000);

        },//人脸检测
        FaceDete_dialog(){
          fs1000a3tdV38.camera.FaceDete();
        },IDCardCompare_Curr(){
          fs1000a3tdV38.camera.IDCardCompare();
        },
        //图图比对
      Base64Compare_Curr(){
        fs1000a3tdV38.camera.Base64Compare_Success(this.base64_1,this.base64_2);
        let this_curr=this;
        setTimeout(function() {
           //获得比对结果
          let cmpResult=fs1000a3tdV38.camera.getCmpresult();
          if(cmpResult>0.7){
            //fileDescribe 1自动比对0人工比对，比对成功，自动上传
            this_curr.SendFaceData(1,cmpResult);
            //this_curr.$parent.DialogPhotoRecord = false;
          }
        },3000);
      },PersonCompare(){
          fs1000a3tdV38.camera.Base64Compare_Success(this.base64_1,this.base64_2);
          let this_curr=this;
          setTimeout(function() {
            //获得比对结果
            let cmpResult=fs1000a3tdV38.camera.getCmpresult();
           //fileDescribe 1自动比对0人工比对，比对成功，自动上传
              this_curr.SendFaceData(0,cmpResult);
              //this_curr.$parent.DialogPhotoRecord = false;

          },3000);

        },//发送人脸识别方法
        SendFaceData(fileDescribe,remark){
          let this_local=this;
          if(! this_local.businessCode || ! this_local.base64_1||! this_local.base64_2){
            this_local.$message('业务编码或照片类型不能为空');
            return ;
          }
          //数据
          let postdata={
            "businessCode":this_local.businessCode,
            "documentTypeCode":3,//人脸识别
            "fileDescribe":fileDescribe,//1自动比对0人工比对
            "remark":remark//自动比对率
          };
          let formData={
            "data":postdata,
            "certificatesImageFile":this_local.base64_1,
            "peopleImageFile": this_local.base64_2
          };
          //文件上传
          axios({
            method: "post",
            baseURL: "api",
            withCredentials: true,
            timeout: 5000 ,
            url: "/api/photo-archival/save-face-detection",
            headers: {
              "Content-Type": "application/json",
              token:store.getters.token?getToken():null
            },
            data: formData
          })
            .then(res => {
              const {data} = res;
              if(data && data.code==1){
                //关闭窗口
                  this_local.$parent.DialogPhotoRecord=false;
              }
              else if ( data && data.message) {
                this_local.$message(data.message)
                return
              }
            }).catch(error => {
            this_local.$message(error)
          });
        }
       },mounted() {
        //当前页面调用ocx对就脚本
       // window.phoneListener = this;
       },  watch: {//监听值变化
        "DialogPhotoRecord"() {
          if (this.DialogPhotoRecord == true) {
            //加载读卡器
            fsIdCard.FsIdCard.onLoad();
            fs1000a3tdV38.camera.onLoad();
            setTimeout(function(){
              // fsPerson.FIOpenVideo();
            },100);
            //FIOpenVideo();
            setTimeout(function(){
              // fsPerson.FIInit();
            },10);
            //需要等视频完全打开后才能检测人脸
            /*  setTimeout("fsPerson.FIStartDetectFace()",2000);*/
            setTimeout(function(){
              // fsPerson.FIStartDetectFace()
            },2000);
          }
        }
      }
    }
</script>
<style>
  .PhotoRecordDialog .el-dialog__title, .PhotoRecordDialog .el-dialog__headerbtn .el-dialog__close {
    color: white;
  }
</style>
<style scoped>
.PageContent{
      margin-top: 0; 
    padding-bottom: 0;
}
</style>
